﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.ComponentModel;
using System.Windows.Forms;
using System.Reflection;
using System.Diagnostics;

using System.IO;
using System.Runtime.Serialization;
using System.Xml.Serialization;

namespace ZMotion
{
     [XmlInclude(typeof(ZMotionCardSetting))]
    public class ZMotionCardSetting
    {
        private int iCardNo;
        [CategoryAttribute("CardInfo")]
        public int CardNo
        {
            get
            {
                return iCardNo;
            }
            set
            {
                iCardNo = value;
            }
        }
        public int[] HomeMode
        {
            get;
            set;

        }
        public double[] Acc
        {
            get;
            set;

        }
        public double[] Dec
        {
            get;
            set;

        }
        public double[] HomeAcc
        {
            get;
            set;

        }
        public double[] HomeDec
        {
            get;
            set;

        }
        public double[] PulsePmm
        {
            get;
            set;

        }
        public int[] AxisType
        {
            get;
            set;

        }
        public int AxisTotalCount
        {
            get;
            set;

        }
        public int TotalSlaveStation
        {
            get;
            set;
        }
        public string IpAddress
        {
            get;
            set;
        }
        private GroupInfo[] groupInfos;
        [CategoryAttribute("GroupInfo")]
        public GroupInfo[] GroupInfoes
        {
            get
            {
                return groupInfos;
            }
            set
            {
                groupInfos = value;
            }
        }
        [XmlIgnore]
        string m_strName;
        public ZMotionCardSetting()
        {
            HomeMode = new int[32];
            Acc = new double[32];
            Dec = new double[32];
            HomeAcc = new double[32];
            HomeDec = new double[32];
            PulsePmm = new double[32];
            AxisType = new int[32];
            for (int i=0;i<32;i++)
            {
                //HomeMode[i] = 0;
                Acc[i] = 0.1;
                Dec[i] = 0.1;
                HomeAcc[i] = 0.1;
                HomeDec[i] = 0.1;
                PulsePmm[i] = 1000;
                HomeMode[i] = 28;
                AxisType[i] = 0;
            }
            groupInfos = new GroupInfo[6];
            for(int i=0;i<6;i++)
            {
                groupInfos[i] = new GroupInfo();
            }
            IpAddress = "192.168.0.11";
        }
       
        public static ZMotionCardSetting Load(string strName)
        {
            ZMotionCardSetting pDoc;
            XmlSerializer xmlSerializer = new XmlSerializer(typeof(ZMotionCardSetting));
            FileStream fsReader = null;
            try
            {
                fsReader = File.OpenRead(Application.StartupPath + @"/MuduleParameter/LeiSaiMCardE5032Setting" + "_" + strName + ".xml");
                pDoc = (ZMotionCardSetting)xmlSerializer.Deserialize(fsReader);
                pDoc.m_strName = strName;
                fsReader.Close();
                if (pDoc.HomeMode == null)
                {
                    pDoc.HomeMode = new int[32];
                }
            }
            catch (IOException)
            {
                if (fsReader != null)
                {
                    fsReader.Close();
                }
                pDoc = new ZMotionCardSetting();
                pDoc.m_strName = strName;
            }
            catch (Exception eMy)
            {
                if (fsReader != null)
                {
                    fsReader.Close();
                }

                MessageBox.Show(eMy.Message + "即将重新加载备份文件", Application.StartupPath + @"/MuduleParameter/LeiSaiMCardE5032Setting" + "_" + strName + ".xml");

                fsReader = File.OpenRead(Application.StartupPath + @"/MuduleParameter/LeiSaiMCardE5032Setting" + "_" + strName + ".xml.bak");
                pDoc = (ZMotionCardSetting)xmlSerializer.Deserialize(fsReader);
                pDoc.m_strName = strName;
                fsReader.Close();

                pDoc.Save();
            }
            return pDoc;
        }
        public bool Save()
        {
            if (!Directory.Exists(System.Windows.Forms.Application.StartupPath + @"/MuduleParameter/"))
            {
                Directory.CreateDirectory(Application.StartupPath + @"/MuduleParameter/");
            }
            FileStream fsWriter = new FileStream(Application.StartupPath + @"/MuduleParameter/LeiSaiMCardE5032Setting" + "_" + m_strName + ".xml", FileMode.Create, FileAccess.Write, FileShare.Read);
            XmlSerializer xmlSerializer = new XmlSerializer(typeof(ZMotionCardSetting));
            xmlSerializer.Serialize(fsWriter, this);
            fsWriter.Flush(true);
            fsWriter.Close();

            File.Copy(Application.StartupPath + @"/MuduleParameter/LeiSaiMCardE5032Setting" + "_" + m_strName + ".xml", Application.StartupPath + @"/MuduleParameter/LeiSaiMCardE5032Setting" + "_" + m_strName + ".xml.bak", true);

            return true;
        }
    }
}
